package com.jk.blog.controller;

import com.jk.blog.model.ResponseInfo;
import com.jk.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 发表文章
     * @return
     */
    @RequestMapping(value="/publish", method = RequestMethod.POST)
    public ResponseInfo publishArticle(String articleTitle, String articleInfo){
        Integer publisherId = 1; // TODO 登录人ID
        // 发布文章
        return articleService.addArticle(articleTitle, articleInfo, publisherId);
    }

    /**
     * 编辑文章
     * @param articleTitle  文章标题
     * @param articleInfo   文章内容
     * @return
     */
    @RequestMapping(value="/edit", method=RequestMethod.POST)
    public ResponseInfo editArticle(String articleTitle, String articleInfo){
        return articleService.updateArticleInfo(articleTitle, articleInfo);
    }

    /**
     * 文章一览、查询
     * @param articleTitle  文章标题，模糊查询
     * @param pageNo        页码
     * @param pageSize      分页大小
     * @return
     */
    @RequestMapping(value="/list", method=RequestMethod.GET)
    @PreAuthorize("hasAnyAuthority('USER')")
    public ResponseInfo articleList(@RequestParam(required = false) String articleTitle, int pageNo, int pageSize){
        return articleService.queryArticleList(articleTitle, pageNo, pageSize);
    }

    /**
     * 文章详情
     * @param id 文章ID
     * @return
     */
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public ResponseInfo articleDetail(@PathVariable Integer id){
        return articleService.queryArticleInfo(id);
    }

    /** q
     * 新增评论
     * @param articleId     文章ID
     * @param commentInfo   评论内容
     * @param commentId     评论ID（回复评论有效）
     * @return
     */
    @RequestMapping(value="/addComment", method=RequestMethod.POST)
    public ResponseInfo addComment(@RequestParam(value = "articleId") Integer articleId,
                                   @RequestParam(value = "commentInfo")  String commentInfo,
                                   @RequestParam(value = "commentId", required = false) Integer commentId){
        Integer createId = 2; // TODO 评论人
        return articleService.addComment(articleId, commentInfo, commentId, createId);
    }
}
